package com.example.miniappssm.service.serviceImpl;

import com.example.miniappssm.bean.GrowthNotes;
import com.example.miniappssm.bean.GrowthRecords;
import com.example.miniappssm.bean.Snaper;
import com.example.miniappssm.mapper.CameraMapper;
import com.example.miniappssm.mapper.GrowthMapper;
import com.example.miniappssm.service.FileService;
import com.example.miniappssm.service.ScheduledService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;

@Service
@Async
@EnableAsync
public class ScheduledServiceImpl implements ScheduledService {

    @Autowired
    CameraMapper cameraMapper;

    @Autowired
    GrowthMapper growthMapper;

    @Autowired
    FileService fileService;

    @Autowired
    RestTemplate restTemplate;

    @Override
//    @Scheduled(cron = "0 0 0 ? 1-12 2,4,6")
    public void autoSnapPicByEZVIZ() throws IOException {

        String accessToken = cameraMapper.selectCameraByCameraId(1).getCameraAccessToken();

        for (GrowthNotes note : growthMapper.selectAllGrowthNotes()) {
            //判断改农作物有没有摄像设备
            if (note.getCropDeviceSerial() != null && !note.getCropDeviceSerial().equals("")) {
                //通过accessToken和deviceSerial获取图片url
                Snaper snaper = new Snaper(accessToken,note.getCropDeviceSerial(),1);
                ResponseEntity<String> responseEntity = restTemplate.postForEntity("https://open.ys7.com/api/lapp/device/capture", snaper, String.class);
                String body = responseEntity.getBody();

                System.out.println(body);

//                String imgUrl = 1;
//                if (imgUrl != null) {
//                    GrowthRecords growthRecords = new GrowthRecords();
//                    growthRecords.setCropId(note.getCropId());
//                    growthRecords.setRecordDate(new Date());
//                    growthRecords.setRecordMain(growthRecords.getRecordDate() + "抓拍图片");
//                    //摄像头抓拍的是网络图片，将网络图片下载到阿里云获得阿里云的地址
//                    growthRecords.setRecordImg(fileService.uploadWithSnapImg(imgUrl));
//                    growthMapper.insertGrowthRecords(growthRecords);
//                }
            }
        }

    }
}
